/*************************************************************************
	> File Name: oj-287_merge_fruits.cpp
	> Author: Wang Bo
	> Mail: 1276106221@qq.com
	> Created Time: 2020年11月04日 星期三 16时48分40秒
 ************************************************************************/

#include <iostream>
#include <set>
using namespace std;

typedef pair<int, int> pile;
void merge_pile(set<pile> &p, int &t);

int main() {
    set<pile> fruit_pile;
    int n, p, sum = 0; 
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> p;
        fruit_pile.insert(make_pair(p, i));
    }
    for (auto it = fruit_pile.begin(); it != fruit_pile.end(); ++it) {
        pile a = *it;
        cout << a.first << " " << a.second << endl;
    }
    while (fruit_pile.size() > 1) {
        merge_pile(fruit_pile, sum);
    }
    cout << sum << endl;
    return 0;
}

void merge_pile(set<pile> &p, int &t) {
    pile a = *(p.begin());
    p.erase(a);
    pile b = *(p.begin());
    p.erase(b);
    cout << "merging " << a.first << " and " << b.first << endl;
    pile c = make_pair(a.first + b.first, a.second);
    p.insert(c);
    t += c.first;
    return ;
}
